export function hasClass(el, token) {
	return new RegExp("(\\s|^)" + token + "(\\s|$)").test(el.className);
}
export function addClass(el, token) {
	if (!el) {
		return;
	}
	if (el.classList) {
		el.classList.add(token);
	} else if (!hasClass(el, token)) {
		el.className += "" + token;
	}
}
export function removeClass(el, token) {
	if (!el) {
		return;
	}
	if (el.classList) {
		el.classList.remove(token);
	} else if (hasClass(el, token)) {
		el.className = el.className
			.replace(new RegExp("(\\s|^)" + token + "(\\s|$)"), " ")
			.replace(/^\s+|\s+$/g, "");
	}
}

/**
 * @param {HTMLElement} element
 * @param {string} event
 * @param {function} handler
 */
export const on = (function () {
	if (document.addEventListener) {
		return function (element, event, handler) {
			if (element && event && handler) {
				element.addEventListener(event, handler, false);
			}
		};
	} else {
		return function (element, event, handler) {
			if (element && event && handler) {
				element.attachEvent("on" + event, handler);
			}
		};
	}
})();
/**
 * @param {HTMLElement} element
 * @param {string} event
 * @param {function} handler
 */
export const off = (function () {
	if (document.removeEventListener) {
		return function (element, event, handler) {
			if (element && event) {
				element.removeEventListener(event, handler, false);
			}
		};
	} else {
		return function (element, event, handler) {
			if (element && event) {
				element.detachEvent("on" + event, handler);
			}
		};
	}
})();

/**
 * @param {HTMLElement} el
 * @param {string} event
 * @param {function} fn
 */
export const once = function (el, event, fn) {
	let listener = function () {
		if (fn) {
			fn.apply(this, arguments);
		}
		off(el, event, listener);
	};
	on(el, event, listener);
};
